package cn.cool.service.impl;

import cn.cool.entity.User;
import cn.cool.entity.UserFollow;
import cn.cool.mapper.UserFollowMapper;
import cn.cool.result.ResponseResult;
import cn.cool.service.UserFollowService;
import cn.cool.utils.JWTUtils;
import cn.cool.utils.RedisUtil;
import cn.cool.vo.FollowFansVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Response;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuH
 * @since 2021-01-23
 */
@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {

    @Autowired
    private UserFollowMapper userFollowMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 用户 关注/取消关注
     *
     * @param request  获取当前用户id
     * @param followId 关注/取消关注 的 目标用户id
     */
    @Override
    public Object setUserFollow(HttpServletRequest request, Integer followId) {
        //获取用户id
        Integer userId = JWTUtils.getUserId(request);

        //用户的关注列表key value-->followId
        String followKey = "follow:" + userId;
        //用户的粉丝列表key value-->userId
        String fansKey = "fans:" + followId;

        boolean isFollow = redisUtil.sHasKey(followKey, followId);

        if (isFollow) { //true已关注 说明此时是要取消关注
            //在关注列表移除 目标id
            redisUtil.setRemove(followKey, followId);
            //在粉丝列表移除 粉丝id
            redisUtil.setRemove(fansKey, userId);
            return ResponseResult.success(200, "取消成功");
        } else {
            //将目标id 添加到当前用户的关注列表中
            redisUtil.sSet(followKey, followId);
            //将当前用户添加到目标用户的粉丝列表中
            redisUtil.sSet(fansKey, userId);
            return ResponseResult.success(200, "关注成功");
        }

    }

    /**
     * 查看关注列表
     *
     * @param request  获取当前用户id
     * @param targetId 目标id
     * @return
     */
    @Override
    public Object getUserOrTargerFollow(HttpServletRequest request, Integer targetId) {
        String key = "follow:";
        if (targetId != null) { // 说明是查看别的用户的关注列表
            key = key + targetId;
            return getFollow(key);
        } else {
            Integer userId = JWTUtils.getUserId(request);
            key = key + userId;
            return getFollow(key);
        }
    }

    /**
     * 查看粉丝列表
     *
     * @param request  获取当前用户id
     * @param targetId 目标id
     * @return
     */
    @Override
    public Object getUserOrTargerFans(HttpServletRequest request, Integer targetId) {
        String key = "fans:";
        if (targetId != null) { // 说明是查看别的用户的关注列表
            key = key + targetId;
            return getFollow(key);
        } else {
            Integer userId = JWTUtils.getUserId(request);
            key = key + userId;
            return getFollow(key);
        }
    }

    /**
     * 查看当前用户与目标用户的 共同关注
     *
     * @param request
     * @param targetId
     * @return
     */
    @Override
    public Object getMutualFollow(HttpServletRequest request, Integer targetId) {
        Integer userId = JWTUtils.getUserId(request);
        //获取两个set集合的交集
        Set<Object> mutualFollowIds = redisUtil.sInter("follow:" + userId, "follow:" + targetId);

        if (mutualFollowIds.size() == 0) {
            return ResponseResult.success(200, "你们还没有共同关注哦!");
        }
        //根据id集合从数据库获取数据
        List<FollowFansVo> followOrFansList = userFollowMapper.getFollowOrFansList(mutualFollowIds);
        return ResponseResult.success(200, "加载成功" , followOrFansList);
    }

    /**
     * 根据key类型 获取用户关注列表
     *
     * @param key
     * @return
     */
    private Object getFollow(String key) {
        //从缓存中获取 目标用户的关注列表/粉丝列表
        Set<Object> followsId = redisUtil.sGet(key);

        if (followsId.size() == 0) {//说明他没有任何关注
            if (key.startsWith("follow:")) {
                return ResponseResult.success(200, "该用户还没有任何关注");
            }
            return ResponseResult.success(200, "该用户还没有任何粉丝");
        }

        List<FollowFansVo> followList = userFollowMapper.getFollowOrFansList(followsId);
        return ResponseResult.success(200, "加载成功", followList);
    }

}
